home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2325 / 2325.xpi / chrome / content / feed.class.js < prev    next >
Text File  |  2009-11-24  |  6KB  |  218 lines

  1. function TickerParseListener() {
  2.     return this;
  3. }
  4.  
  5. TickerParseListener.prototype = {
  6.     handleResult: function(result) {
  7.         var resolvedUri = result.uri.resolve("");
  8.         var feedDataKey = resolvedUri.toLowerCase();
  9.         
  10.         if (!result.bozo) {
  11.             var feed = result.doc;
  12.         
  13.             if (feed) {
  14.                 try {
  15.                     feed.QueryInterface(Components.interfaces.nsIFeed);
  16.         
  17.                     var feedObject = {
  18.                         label : "",
  19.                         image : "",
  20.                         description : "",
  21.                         uri : "",
  22.                         siteUri : "",
  23.                         items : [],
  24.                         id : "",
  25.                         rootUri : ""
  26.                     };
  27.         
  28.                     feedObject.id = resolvedUri;
  29.                     feedObject.uri = resolvedUri;
  30.         
  31.                     try {
  32.                         feedObject.siteUri = feed.link.resolve("");
  33.                     } catch (e) {
  34.                         feedObject.siteUri = feedObject.uri;
  35.                     }
  36.                     
  37.                     var parts = feedObject.siteUri.split("/");
  38.                     
  39.                     try {
  40.                         feedObject.rootUri = parts[0] + "//" +  parts[2] + "/";
  41.                     } catch (e) {
  42.                         feedObject.rootUri = "";
  43.                     }
  44.         
  45.                     feedObject.label = feed.title.plainText();
  46.         
  47.                     if (!feedObject.label) {
  48.                         feedObject.label = feed.title.plainText();
  49.                     }
  50.         
  51.                     if (feed.summary && feed.summary.text) {
  52.                         feedObject.description = feed.summary.text;//plainText();
  53.                     }
  54.                     else if (feed.content && feed.content.text) {
  55.                         feedObject.description = feed.content.text;//plainText();
  56.                     }
  57.                     else if (feed.subtitle && feed.subtitle.text) {
  58.                         feedObject.description = feed.subtitle.text;
  59.                     }
  60.                     else {
  61.                         feedObject.description = "No summary."; // TODO
  62.                     }
  63.                     
  64.                     feedObject.description = tickerEntityDecode(feedObject.description);
  65.                     feedObject.label = tickerEntityDecode(feedObject.label);
  66.                     
  67.                     feedObject.image = feedObject.siteUri.substr(0, (feedObject.siteUri.indexOf("/", 9) + 1)) + "favicon.ico";
  68.         
  69.                     var numItems = feed.items.length;
  70.         
  71.                     for (var i = 0; i < numItems; i++) {
  72.                         var item = feed.items.queryElementAt(i, Components.interfaces.nsIFeedEntry);
  73.             
  74.                         var itemObject = {
  75.                             uri : "",
  76.                             published : "",
  77.                             label : "",
  78.                             description : "",
  79.                             image : "",
  80.                             id : ""
  81.                         };
  82.             
  83.                         try {
  84.                             itemObject.id = item.id;
  85.                 
  86.                             itemObject.uri = item.link.resolve("");
  87.                             
  88.                             /*
  89.                             if (itemObject.uri.match(/\/\/news\.google\.com\/.*\?/)){
  90.                                 var q = itemObject.uri.indexOf("?");
  91.                                 itemObject.uri = itemObject.uri.match(/url=(https?:\/\/.*)$/i)[1];
  92.                                 itemObject.uri = decodeURIComponent(itemObject.uri.split("&")[0]);
  93.             //                    itemObject.uri = itemObject.uri.substring(0, q) + ("&" + itemObject.uri.substring(q)).replace(/&(ct|cid|ei)=[^&]* /g, "").substring(1);
  94.                             }
  95.                             */
  96.                             
  97.                             if (!itemObject.id) itemObject.id = itemObject.uri;
  98.                 
  99.                             if (!itemObject.uri.match(/\/~r\//i)) {
  100.                                 /*
  101.                                 if (itemObject.uri.match(/\/\/news\.google\.com\//)){
  102.                                     // Google news
  103.                                     var root = itemObject.uri.match(/url=(https?:\/\/[^\/]+\/)/i)[1];
  104.                                     itemObject.image = root + "favicon.ico";
  105.                                     delete root;
  106.                                 }
  107.                                 else {
  108.                                     */
  109.                                     itemObject.image = itemObject.uri.substr(0, (itemObject.uri.indexOf("/", 9) + 1)) + "favicon.ico";
  110.                                     /*
  111.                                 }
  112.                                 */
  113.                             }
  114.                             else {
  115.                                 // Feedburner
  116.                                 itemObject.image = feedObject.siteUri.substr(0, (feedObject.siteUri.indexOf("/", 9) + 1)) + "favicon.ico";
  117.                             }
  118.                             
  119.                             itemObject.published = Date.parse(item.updated);
  120.                             
  121.                             if (!itemObject.published) {
  122.                                 itemObject.published = new Date().getTime();
  123.                             }
  124.                             
  125.                             if (item.title) {
  126.                                 itemObject.label = item.title.plainText();
  127.                             }
  128.                             else {
  129.                                 itemObject.label = item.updated;
  130.                             }
  131.                 
  132.                             itemObject.label = itemObject.label.replace(/\s+/, " ");
  133.                 
  134.                             if (item.summary && item.summary.text) {
  135.                                 itemObject.description = item.summary.text;
  136.                             }
  137.                             else if (item.content && item.content.text) {
  138.                                 itemObject.description = item.content.text;
  139.                             }
  140.                             else {
  141.                                 itemObject.description = "No summary."; // TODO
  142.                             }
  143.                             
  144.                             itemObject.description = tickerEntityDecode(itemObject.description);
  145.                             itemObject.label = tickerEntityDecode(itemObject.label);
  146.                             
  147.                             if (item.enclosures && item.enclosures.length > 0) {
  148.                                 var len = item.enclosures.length;
  149.                                 var imgs = "";
  150.                     
  151.                                 for (var j = 0; j < len; j++) {
  152.                                     var enc = item.enclosures.queryElementAt(j, Components.interfaces.nsIWritablePropertyBag2);
  153.                         
  154.                                     if (enc.hasKey("type") && enc.get("type").indexOf("image") != 0) {
  155.                                         imgs += '<br /><a href="' + enc.get("url") + '">Download</a>'; // TODO
  156.                                     }
  157.                                     else if (enc.hasKey("url")) {
  158.                                         imgs += '<br /><img src="' + enc.get("url") + '" />';
  159.                                     }
  160.                                 }
  161.                     
  162.                                 itemObject.description = itemObject.description + imgs;
  163.                     
  164.                                 delete len;
  165.                                 delete imgs;
  166.                             }
  167.                 
  168.                             itemObject.description = itemObject.description.replace(/<script[^>]*>[\s\S]+<\/script>/gim, "");
  169.                 
  170.                             itemObject.visited = RSSTICKER.history.isVisitedURL(itemObject.uri, itemObject.id, "q");
  171.                 
  172.                             feedObject.items.push(itemObject);
  173.                         } catch (e) {
  174.                             RSSTICKER.logMessage(e);
  175.                         }
  176.             
  177.                         delete item;
  178.                         delete itemObject;
  179.                     }
  180.     
  181.                     delete unread;
  182.                     delete resolvedUri;
  183.                     delete feedDataKey;
  184.                     delete feed;
  185.                     delete numItems;
  186.                     delete result;
  187.                 } catch (e) {
  188.                     RSSTICKER.logMessage(e);
  189.                 }
  190.             }
  191.         }
  192.         
  193.         RSSTICKER.writeFeed(feedObject);
  194.         
  195.         delete feedObject;
  196.     }
  197. };
  198.  
  199. function tickerEntityDecode(aStr) {
  200.     var    formatConverter = Components.classes["@mozilla.org/widget/htmlformatconverter;1"].createInstance(Components.interfaces.nsIFormatConverter);
  201.     var fromStr = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
  202.     fromStr.data = aStr;
  203.     var toStr = { value: null };
  204.  
  205.     try {
  206.         formatConverter.convert("text/html", fromStr, fromStr.toString().length, "text/unicode", toStr, {});
  207.     } catch(e) {
  208.         return aStr;
  209.     }
  210.     
  211.     if(toStr.value) {
  212.         toStr = toStr.value.QueryInterface(Components.interfaces.nsISupportsString);
  213.         return toStr.toString();
  214.     }
  215.     
  216.     return aStr;
  217. }
  218.